package cn.study.concurrenthashmap.unsafe;

import sun.misc.Unsafe;

/**
 * @Author     ：跟大佳一起学架构
 * @Description：如何实现一个cas
 */
public class AtomicIncrNumUpdater {

    private static volatile int [] age = new int[]{};

    public AtomicIncrNumUpdater(int [] age){
        this.age = age;
    }

    public int [] getAge(){
        return this.age;
    }

    private static final Unsafe unsafe = UnsafeInstance.reflectGetUnsafe();


    public static void main(String[] args) {
        int arrayBaseOffset = unsafe.arrayBaseOffset(int[].class);
        long indexScale = unsafe.arrayIndexScale(age.getClass());
        unsafe.putOrderedObject(age, arrayBaseOffset+(indexScale * 1), 18);
        unsafe.putOrderedObject(age, arrayBaseOffset+(indexScale * 4), 38);
        System.out.println("大佳的真实年龄---"+unsafe.getObject(age, arrayBaseOffset+(indexScale * 1)));
        System.out.println("老于的真实年龄---"+unsafe.getObjectVolatile(age, arrayBaseOffset+(indexScale * 4)));
    }
}
